<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Apache默认虚拟主机 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Apache默认虚拟主机</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Apache/">Apache</a> <a class="article__tag-link" href="/tags/LAMP/">LAMP</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为了防止其他不是自己的域名解析到自己的IP,我们可以通过更改虚拟主机文件限制其他域名。我们创建的新的虚拟主机，创建一个新的空目录将其权限设置为600。这样只有我们在配置文件中定义的域名在能访问。<br>命令如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># vim /usr/local/apache2/conf/extra/httpd-vhosts.conf</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;把下面的配置：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">&lt;VirtualHost *:80&gt;</div><div class="line">  ServerAdmin webmaster@dummy-host.example.com</div><div class="line">  DocumentRoot <span class="string">"/usr/local/apache2/docs/dummy-host.example.com"</span></div><div class="line">  ServerName dummy-host.example.com</div><div class="line">  ServerAlias www.dummy-host.example.com</div><div class="line">  ErrorLog <span class="string">"logs/dummy-host.example.com-error_log"</span></div><div class="line">  CustomLog <span class="string">"logs/dummy-host.example.com-access_log"</span> common</div><div class="line">&lt;/VirtualHost&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">&lt;VirtualHost *:80&gt;</div><div class="line">  DocumentRoot <span class="string">"/tmp/tmp"</span></div><div class="line">  ServerName tmp.com</div><div class="line"> &lt;Directory /tmp/tmp/&gt;</div><div class="line">  Order allow,deny</div><div class="line">  Deny from all</div><div class="line"> &lt;/Directory&gt; </div><div class="line">&lt;/VirtualHost&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以直接设置成第一个虚拟主机配置文件。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;创建配置中提到的目录，并设置权限 600</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># mkdir /tmp/tmp</span></div><div class="line">[root@lamp ~]<span class="comment"># chmod 600 /tmp/tmp</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;检测配置文件，重启 apache 服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@lamp ~]<span class="comment"># apachectl -t</span></div><div class="line">Syntax OK</div><div class="line">[root@lamp ~]<span class="comment"># apachectl restart</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其实这个默认虚拟主机就是配置文件里的第一个虚拟主机。关于默认虚拟主机有个特点，凡是解析到这台机器的域名，不管是什么域名，只要在配置文件中没有配置，那么都会访问到这个主机上来。为了避免别人乱解析，应该把默认也就是第一个虚拟主机给禁止掉。这里使用了 allow，deny 语句，已经禁止掉了。现在用 ip 或其他域名去访问，已经提示：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/Apache%E9%BB%98%E8%AE%A4%E8%99%9A%E6%8B%9F%E4%B8%BB%E6%9C%BA/01.png?raw=true" alt=""></figure></p>
<h2 id="apache-nameserver-两个如何设置"><a href="#apache-nameserver-两个如何设置" class="headerlink" title="apache nameserver 两个如何设置"></a>apache nameserver 两个如何设置</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">ServerName sub1.domain.com</div><div class="line">ServerAlias sub2.domain.com</div><div class="line">DocumentRoot /var/www/sub/</div><div class="line"><span class="comment"># other settings</span></div></pre></td></tr></table></figure>
<h2 id="apache-代理模块-proxy应用"><a href="#apache-代理模块-proxy应用" class="headerlink" title="apache 代理模块 proxy应用"></a>apache 代理模块 proxy应用</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;要使用proxy功能，首先编译apache的时候，需要加上参数 ·–enable-proxy –enable-proxy-http·</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果，不想重新编译，也可以以扩展的形式安装proxy模块，具体请参考 <a href="https://hcldirgit.github.io/2017/07/27/11.%20apache%20mod_proxy%20%E6%89%A9%E5%B1%95%E6%A8%A1%E5%9D%97%E5%AE%89%E8%A3%85/" target="_blank" rel="external">apache mod_proxy 扩展模块安装</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;具体配置为：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">ServerName www.test.com</div><div class="line">CustomLog <span class="string">"/dev/null"</span> combined</div><div class="line">ProxyRequests Off</div><div class="line"></div><div class="line">    Order deny,allow</div><div class="line">    Allow from all</div><div class="line"> </div><div class="line">ProxyPass / http://www.test.com/</div><div class="line">ProxyPassReverse / 192.168.13.111/</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;另外需要注意的是，需要在/etc/hosts 中加一条记录  </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">192.168.13.111   www.test.com</div></pre></td></tr></table></figure>
<h2 id="apache某个虚拟主机不启用gzip"><a href="#apache某个虚拟主机不启用gzip" class="headerlink" title="apache某个虚拟主机不启用gzip"></a>apache某个虚拟主机不启用gzip</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;默认情况下，如果apache中调用了mod_deflate ，那么所有虚拟主机都会启用gzip压缩功能。但有时候会遇到某个虚拟主机不想启用gzip的应用，这如何设置呢？<br>只要在该虚拟主机中加入一下代码即可。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">SetOutputFilter DEFLATE</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里的 /data/web/example 为该虚拟主机的主目录。</p>
<h2 id="apache配置多个域名指向的虚拟主机访问总是指向第一个虚拟主机"><a href="#apache配置多个域名指向的虚拟主机访问总是指向第一个虚拟主机" class="headerlink" title="apache配置多个域名指向的虚拟主机访问总是指向第一个虚拟主机"></a>apache配置多个域名指向的虚拟主机访问总是指向第一个虚拟主机</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在配置apache多个虚拟主机的时候，访问总是指向第一个虚拟主机,加上 NameVirtualHost * :80后解决问题.</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">&lt;VirtualHost *:80&gt;</div><div class="line"> DocumentRoot <span class="string">"F:/web"</span> </div><div class="line"> ServerName localhost</div><div class="line">&lt;/VirtualHost&gt;</div><div class="line">&lt;VirtualHost *:80&gt;</div><div class="line"> DocumentRoot <span class="string">"F:/web/aa/"</span> </div><div class="line"> ServerName *.test.com</div><div class="line">&lt;/VirtualHost&gt;</div></pre></td></tr></table></figure>
<h2 id="针对apache的虚拟主机开启php短标签"><a href="#针对apache的虚拟主机开启php短标签" class="headerlink" title="针对apache的虚拟主机开启php短标签"></a>针对apache的虚拟主机开启php短标签</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;php.ini中</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">short_open_tag = On</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;除&lt;?php ?&gt;，可使用更灵活的调用方法</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&lt;? /<em>程序操作</em>/ ?&gt;</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&lt;?=/<em>函数</em>/?&gt;</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Apache/24. Apache默认虚拟主机/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
